<?php

namespace admin\controllers;

use admin\components\BaseAuthController;
use app\models\Setting;

class SettingController extends BaseAuthController {

    public $name = '系统设置';

    public function actionIndex() {
        $model = new Setting();
        if (!isset($_POST[$model->formName()])) {
            return $this->render(compact('model'));
        }

        $data = $_POST[$model->formName()];
        foreach ($data as $name => $val) {
            if (!($ar = Setting::findOne(['id' => $name]))) {
                $ar = new Setting();
                $ar->id = $name;
            }
            $ar->val = $val;
            $res = $ar->save(FALSE);
            if ($res['code'] != 0) {
                return $res;
            }
        }

        return $this->success();
    }
}
